<?php
namespace App\Models\AppModels;
use App\Models\ParentModel;
use DB;
/**
 * 用户监控model
 * @author puppet
 */
class Userinfo extends ParentModel{
	/**
	* 与模型关联的数据表。
	*
	* @var string
	*/
	protected $table = 'pt_user_info';

	/**
	* DB::直接操作的数据库表名
	* @var string
	*/
	protected $db_table = 'pt_user_info';

	/**
	* 主键 默认id。
	*
	* @var string
	*/
	protected $primaryKey = 'id';

	/**
	* 指定是否模型应该被戳记时间。
	*
	* @var bool
	*/
	public $timestamps = false;
	
	
	/**
	*	获取用户监控数据
	*	$type 1(主页用户)  2(单视频用户) 3(广告用户)   $page 第几页   $role_id角色id   partner_id登录的ID
	*/
	function selectData($parames){
		$partner_id=isset(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : 0;
		$type=isset($parames['type']) ? intval($parames['type']) : 0;
		
		$size = (isset($parames['limit']) && intval($parames['limit'])>0 && intval($parames['limit']) <= 100) ? $parames['limit'] : 10;
		$page = isset($parames['page']) ? $parames['page'] : 1;
		$offset = $size * ($page - 1);
		
		$countSql="select count(id) as count from $this->table where info_type = ? and partner_id = {$partner_id} and system_type = 0";
		$sql="select * from $this->table where info_type = ? and partner_id = {$partner_id} and system_type = 0";
		
		if(isset($parames['name']) && !empty($parames['name']) ){
			$countSql.=" and info_name like '%{$parames['name']}%'";
			$sql.=" and info_name like '%{$parames['name']}%'";
		}
		
		if($type==1){
			$sql.=" order by status desc,info_date desc limit $offset,$size";
		}else{
			$sql.=" order by info_date desc limit $offset,$size";
		}
		
		// echo $countSql;exit;
		$count=DB::select($countSql,[$type]);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$data=DB::select($sql,[$type]);
		if($type==1){//主页
			foreach($data as $k=>$v){
				$data[$k]['info_signature']="<div style='max-width:200px;max-height:80px;overflow:auto;'>{$data[$k]['info_signature']}<div>";
				$data[$k]['img_url']="<a href='{$v['info_vurl']}' target='_blank'><img src='{$v['info_img']}' width='70' height='70'></a>";
				
				$num_fls=$num_sop="";
				//关注/粉丝数
				$num_fls.="<span style='color:red;font-size:18px;'>".$v['info_follow']."</span> 关注<br/><br/>";
				$num_fls.="<span style='color:red;font-size:18px;'>".($v['info_fans']>=10000 ? round($v['info_fans']/10000,1)." W" : $v['info_fans'])."</span> 粉丝";
				$data[$k]['num_fls']=$num_fls;
				
				//作品/获赞数
				$num_sop.="<span style='color:red;font-size:18px;'>".$v['info_opus']."</span> 作品<br/><br/>";
				$num_sop.="<span style='color:red;font-size:18px;'>".($v['info_support']>=10000 ? round($v['info_support']/10000,1)." W" : $v['info_support'])."</span> 赞";
				$data[$k]['num_sop']=$num_sop;
				
				$Ccount=DB::select("select count(id) as count from pt_user_info_comment where type=? and partner_id={$partner_id} and pt_type=1",[$v['id']]);
				$Ccount=isset($Ccount[0]['count']) ? $Ccount[0]['count'] : 0;
				$staTime=date('Y-m-d',strtotime(date('Y-m-d',time()-24*3600)))." 22:30:00";
				$Ncount=DB::select("select count(id) as count from pt_user_info_comment where type=? and partner_id={$partner_id} and pt_type=1 and update_time>='{$staTime}'",[$v['id']]);
				$Ncount=isset($Ncount[0]['count']) ? $Ncount[0]['count'] : 0;
				$data[$k]['allCount']="{$Ccount} / {$Ncount}";
				
				$statusText=$v['status']==0 ? 'default' : 'checkbox';
				$checkedText=$v['status']==0 ? '' : 'checked';
				$data[$k]['status']="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"{$statusText}\" checked=\"{$checkedText}\" onclick=\"infoStatus({$v['id']},this)\"><span></span></label></div></td>";
				
				$title="账户昵称：".$v['info_name'];
				$data[$k]['operation']="<a href=\"javascript:;\" title=\"查看评论\"><span class=\"glyphicon glyphicon-eye-open\" onclick=\"showContent({$v['id']},'{$title}')\"></span></a>";
			}
		}else if($type==2 || $type==3){//视频、广告
			foreach($data as $k=>$v){
				$data[$k]['info_signature']="<div style='max-width:200px;max-height:100px;overflow:auto;'>{$data[$k]['info_signature']}<div>";
				$data[$k]['img_url']="<a href='{$v['info_vurl']}' target='_blank'><img src='{$v['info_img']}' width='70' height='100'></a>";
				
				$num_fls=$num_sop=$num_fans="";
				//评论数
				if($v['info_opus']>=10000){
					$num_fls="<span style='color:red;font-size:18px;'>".round($v['info_opus']/10000,1)." W</span> 评论";
				}else{
					$num_fls="<span style='color:red;font-size:18px;'>".$v['info_opus']." </span> 评论";
				}
				$data[$k]['num_fls']=$num_fls;
				
				//获赞数
				if($v['info_support']>=10000){
					$num_sop="<span style='color:red;font-size:18px;'>".round($v['info_support']/10000,1)." W</span> 赞";
				}else{
					$num_sop="<span style='color:red;font-size:15px;'>".$v['info_support']."</span> 赞";
				}
				$data[$k]['num_sop']=$num_sop;
				
				//分享数
				if($v['info_fans']>=10000){
					$num_fans.="<span style='color:red;font-size:18px;'>".round($v['info_fans']/10000,1)." W</span> 次分享";
				}else{
					$num_fans.="<span style='color:red;font-size:15px;'>".$v['info_fans']."</span> 次分享";
				}
				$data[$k]['num_fans']=$num_fans;
				
				//评论数
				$Ccount=DB::select("select count(id) as count from pt_user_info_comment where type=? and partner_id={$partner_id} and pt_type=1 and video_id='{$v['number_id']}'",[0]);
				$data[$k]['allCount']=isset($Ccount[0]['count']) ? $Ccount[0]['count'] : 0;
				
				$title="视频标题：".$v['info_signature'];
				$data[$k]['operation']="<a href=\"javascript:;\" title=\"查看评论\"><span class=\"glyphicon glyphicon-eye-open\" onclick=\"showContent('{$v['number_id']}','{$title}')\"></span></a>";
			}
		}
		
		return ['data'=>$data,'total'=>$count];
	}
	
	//插入、更新用户监控数据
	function insertUser($data,$values){
		$select_sql="select id from pt_user_info where info_type = ? and number_id = ? and system_type = ? and partner_id = ? limit 1";
		$re=DB::select($select_sql,$values);
		$re=isset($re[0]) ? $re[0]['id'] : false;
		
		$sqlAll="update pt_user_info set ";
		foreach($data as $k=>$v){
			$sqlAll.="`{$k}`='{$v}',";
		}
		//更新该 number_id 的所有数据
		$sqlAll=substr($sqlAll,0,-1)." where number_id='{$values[1]}'";
		DB::update($sqlAll);
		
		if(!$re){
			$data['info_type']=$values[0];
			$data['number_id']=$values[1];
			$data['system_type']=$values[2];
			$data['partner_id']=$values[3];
			$getId=DB::table($this->table)->insertGetId($data);
		}
		return $re ? $re : $getId;
	}
	
	
	
	//拉取数据20条	type	0视频，否则主页
	function getVideoComment($video_id,$type=0,$page=1,$hour,$day,$week){
		$user_id=isset(session('vip_user')[0]['admin_id']) ?  intval(session('vip_user')[0]['admin_id']) : 0;
		$size = 20;
		$offset = $size * ($page - 1);
		if(!$type){
		    $countsql="select count(id) as count from pt_user_info_comment where type=0 and partner_id={$user_id} and video_id=?";
            $sql="select * from pt_user_info_comment where type=0 and partner_id={$user_id} and video_id=? ";
            if($hour==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1 hours' ));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($day==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1day'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($week==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1week'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }
			$count=DB::select($countsql,[$video_id]);
			$count=isset($count[0]['count']) ? $count[0]['count'] : 0;
			$sql.=" order by create_time desc  limit {$offset},{$size}";
			$re=DB::select($sql,[$video_id]);
			$re=isset($re[0]['id']) ? $re : false;
		}else{
		    $countsql="select count(id) as count from pt_user_info_comment where type={$type} and partner_id=?";
		    $sql="select * from pt_user_info_comment where type={$type} and partner_id=?";
            if($hour==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1 hours' ));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($day==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1day'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($week==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1week'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }
		    $sql.=" order by create_time desc limit {$offset},{$size}";
			$count=DB::select($countsql,[$user_id]);
			$count=isset($count[0]['count']) ? $count[0]['count'] : 0;
			$re=DB::select($sql,[$user_id]);
			$re=isset($re[0]['id']) ? $re : false;
		}

		$html="";
		$nextPage=$page < ceil($count/$size) ? $page+1 : 0;
		if($re){
			foreach($re as $k=>$v){
//                $sj = date('Y-m-d',$v['update_time']);
				$html.="<tr>";
					$html.="<td><a href=\"https://www.douyin.com/user/{$v['sec_uid']}\" target=\"_blank\"><img src=\"{$v['userImg']}\" width=\"50px\" height=\"50px\"></a></td>";
					$html.="<td style=\"white-space:normal;min-width:150px;word-wrap:break-word;word-break:break-all;\">{$v['content']}</td>";
					$html.="<td>{$v['nickname']}</td>";
					$html.="<td>{$v['user_id']}<br/>".str_replace(["\r\n","\n","\r"],'<br/>',$v['tells'])."</td>";
					$html.="<td><a href=\"{$v['video_url']}\" target=\"_blank\">".mb_substr($v['video_url'],0,20)."...".mb_substr($v['video_url'],-10)."</a></td>";
                    $html.="<td>".mb_substr($v['create_time'],0,22)."</td>";
                    $html.="<td>".mb_substr($v['update_time'],0,10)."</td>";
					$statusText=$v['status']==0 ? "default" : "checkbox";
					$checkedText=$v['status']==0 ? "" : "checked";
					$html.="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"{$statusText}\" checked=\"{$checkedText}\" onclick=\"upStatus({$v['id']},this)\"><span></span></label></div></td>";
					$follow="snssdk1128://user/profile/".$v['uid'];
					$html.="<td><a href=\"javascript:void(0);\" title=\"扫码私聊\"><span class=\"btn btn-info\" onclick=\"following('{$follow}')\">扫码私聊</span></a></td>";
				$html.="</tr>";
			}
		}
		return ["html"=>$html,'nextPage'=>$nextPage];
	}
	
	
	//查询用户套餐类型
	function selectMeal($partner_id){
		$re=DB::select("select meal_name from pt_partner_meal where partner_id=?",[$partner_id]);
		return isset($re[0]['meal_name']) ? $re[0]['meal_name'] : 'first';
	}
	
	
	
	
	
	
	
}